<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head> 
    <meta charset="UTF-8">
    <%@ include file="/WEB-INF/views/include/head.jsp"%>
    <link rel="stylesheet" href="${ctxStatic}/css/common_erp.css">
    <link rel="stylesheet" href="${ctxStatic}/css/layerSm.css">
    <script type="text/javascript">
     $(document).ready(function() {
    	 $("#customSel").select2();
    	 $(".select2-choice")[0].style.backgroundColor = "#f1f3f2";
    	showAddRowBtn();
    	if ($("#id").val()!=null&&$("#id").val()!="") {
    		selectCartonCallBack("${enchase.commaCartonId}",1);
    		
		}
    });
    
    // 添加行按钮制御
    function showAddRowBtn(){
    	if($("#tableIndentList tr").length>0 && $("#tableCartonList tr").length>0){
    		$("#btnAddRow").show();
    		$("#btnSave").show();
    	}else{
    		$("#btnAddRow").hide();
    		$("#btnSave").hide();
    	}
    }
    
    // 选择纸箱回调
    function selectCartonCallBack(commaCartonId,flag){
    	if (flag==1) {
	    	$("#activity_pane").showLoading();
		}
		
			$("#commaCartonId").val(commaCartonId);
			$('#tablePackageDetailsList').html("");
			$('#tableIndentList').html("");
	    	// 格式化，去除逗号
	 	    var commaCartonId = $("#commaCartonId").val();
	        // 调用后台更新数据
	        $.post("${ctx}/customenchase/single/addCarton?commaCartonId="+commaCartonId,"",function(result) {
	            // 回调函数赋值
	            var h = $(callBackHtml(result)).find("#tableCartonList").html();
	            $('#tableCartonList').html(h);
	            
		    	// 添另行按钮制御
		    	showAddRowBtn();
		    	if (flag==1) {
			    	enchaseCloud();
		    	}
	        }, 'html');
    }
    
    // 合同订单选择回调
    function selectIndentCallBack(indentId){
		if(indentId != null && indentId != '' && indentId != "undefined"){
		        // 调用后台更新数据
		        $.post("${ctx}/enchase/single/indentList?indentId="+indentId,'',function(result) {
		            // 回调函数赋值
		            var h = $(callBackHtml(result)).find("#tableIndentList").html();
		            $('#tableIndentList').html(h);
		            
		            var h = $(callBackHtml(result)).find("#trHeadPackageDetailsSize").html();
		            $('#trHeadPackageDetailsSize').html(h);
		            
		            // 回调函数赋值
		            var h = $(callBackHtml(result)).find("#tablePackageDetailsList").html();
		            $('#tablePackageDetailsList').html(h);
		            
		            getStyleInfoAllColor();
		            
		            $("select").select2();
		            
		            cartonAllQuantity();
		            
		            sumAllValue();
		            
			    	// 添另行按钮制御
			    	showAddRowBtn();
		        }, 'html');
		}
    }
    
    // 取得款式信息里所有的颜色
    function getStyleInfoAllColor() {
    	var arr =  $("#tableIndentList").find(".styleInfoColor");
    	var arrList = new Array();
    	if(arr!=null && arr.length>0){
 	    	$.each(arr, function(i, item){
	    		if($.inArray($(this).text(),arrList)==-1){
	    		   arrList.push($(this).text());
	    		}
	    	});
    	}
    	$("#colorList").val(arrList);
    }
    
    // 添加装箱单明细
    function addRowPackageDetails(){
    	    getStyleInfoAllColor();
	 	    var params = $("#inputForm").serialize();
	        // 调用后台更新数据
	        $.post("${ctx}/enchase/single/addRowPackageDetails",params,function(result) {
	            // 回调函数赋值
	            var h = $(callBackHtml(result)).find("#tablePackageDetailsList").html();
	            $('#tablePackageDetailsList').html(h);
	            
	            $("select").select2();
	            onClickTd();
	            showAddRowBtn();
	        }, 'html');
    }
    
    // 单击箱体明细尺码
	function onClickTd() {
	    $("#tablePackageDetailsList").find("td[class^='subSize']").on("click",function(){
	    	
	    	// ***尺码起始位置
	    	var sizeIndext = 12;
	    	
	        var index = $(this).parent().index();
            var cellIndex = $(this)[0].cellIndex;
            
            var gColorName = $("#tablePackageDetailsList tr").eq(index).find(".selectPackageListColor").eq(1).val();
            var gSizeName = $("#trHeadPackageDetailsSize th").eq(cellIndex-sizeIndext).text();

	    	if(!hasColorSize(gColorName,gSizeName)){
	    		return false;
	    	}
	    	
	    	//找到当前鼠标单击的td
	        var tdObj = $(this);
	        //创建一个文本框
	        var inputTemp = "<input tyle = 'text' value = '"+$(this).text()+"'  maxlength = '4'>";
	        var inputObj = $(inputTemp);
	        $(inputObj).keyup(function(){
	        	this.value=this.value.replace(/\D/g,'');
	        });
            
	        //去掉文本框的边框  
	        inputObj.css("border-width", 0);
	        inputObj.click(function () {
	            return false;
	        });
	        //使文本框的宽度和td的宽度相同  
	        inputObj.width(tdObj.width()-5);
	        inputObj.height(tdObj.height()-14);;
	        //去掉文本框的外边距 
	        inputObj.css("margin", 0);
	        inputObj.css("padding", 0);
	        //把文本框放到td中  
	        tdObj.html(inputObj);
	        //文本框失去焦点的时候变为文本  
	        inputObj.blur(function () {
	            var newText = $(this).val();
	            $("#tablePackageDetailsList tr").eq(index).find(".hiddenSubSize").eq(cellIndex-sizeIndext).val(newText);
	            tdObj.html(newText);
	            oneCartonQuantity(index);
	        });
	        //全选  
	        inputObj.trigger("focus").trigger("select");
	   });
	}
    
    // 判断颜色尺码
    function hasColorSize(fColorName,fSizeName) {

         var number = 0;
         // 从页面获取值 进行比较
         $("#tableIndentList tr").each(function(j){
          if ((fColorName == $(this).find('.styleInfoColor').text()) && (fSizeName == $(this).find('.styleInfoSizeName').text())) {
                 number = number+1;
          }
         });
        
         if (number> 0) {
            return true;
         } else {
            return false;
         }
    }
    
    // 颜色变更后，所有尺码清空
    function clearAllSizeNum(trIndex){
    	$("#tablePackageDetailsList tr").eq(trIndex).find(".hiddenSubSize").val('');
    	$("#tablePackageDetailsList tr").eq(trIndex).find("[class^='subSize']").text('');
    	
    	oneCartonQuantity(trIndex);
    }
    
    // 格式化
    function formatInputNumber(inputObj,trIndex,decimalPoint){
    	var replaceVal = 0;
    	// 有小数
    	if(decimalPoint>0){
	       var strRegex = /^(-?\d+)(\.\d+)?$/;
	       if(strRegex.test($(inputObj).val())){
	    	   replaceVal = $(inputObj).val();
	       }
    	}else{
    		if($(inputObj).val().replace(/\D/g,'') != ''){
         	   replaceVal = $(inputObj).val().replace(/\D/g,'');
    		}
    	}
    	
    	$(inputObj).val(replaceVal);
    }
    
    // 删除箱单明细
    function deletePackageDetials(trIndex){
    	getStyleInfoAllColor();
    	$("#trIndex").val(trIndex);
    	var fFrom = parseInt($("#tablePackageDetailsList tr").eq(trIndex).find(".inputPackageListCartonNoFrom").val());
    	var fTo = parseInt($("#tablePackageDetailsList tr").eq(trIndex).find(".inputPackageListCartonNoTo").val());
 	    var params = $("#inputForm").serialize();
        // 调用后台更新数据
        $.post("${ctx}/enchase/single/delPackageDetail",params,function(result) {
            // 回调函数赋值
            var h = $(callBackHtml(result)).find("#tablePackageDetailsList").html();
            $('#tablePackageDetailsList').html(h);
            
            $("select").select2();
            onClickTd();
            
        	// 设定箱号存在flg
        	setSameCartonNoFlg(trIndex,fFrom,fTo);
            
            // 待装箱
            waitCartonQuantity();
            // 纸箱数
            cartonAllQuantity();
        	// 计算总值
        	sumAllValue();
        	
            if($("#tablePackageDetailsList tr").length==0){
        		$("#btnSave").hide();
        		tipParent("无数据");
            }
            
        }, 'html');
    }
    
    
    // 净重改变
    function changeNetWeight(inputObj,trIndex){
    	// 格式化
    	formatInputNumber(inputObj,trIndex,5);
    	
    	// 计算总值
    	sumAllValue();
    }
    
    // 改变箱数
    function changeDetailsCartonNumber(trIndex){
    	var fFrom = parseInt($("#tablePackageDetailsList tr").eq(trIndex).find(".inputPackageListCartonNoFrom").val());
    	var fTo = parseInt($("#tablePackageDetailsList tr").eq(trIndex).find(".inputPackageListCartonNoTo").val());
    	if(fFrom==0 || fTo==0){
    		return;
    	}
    	$("#tablePackageDetailsList tr").eq(trIndex).find(".tdPackageListCartonQuantity").text(fTo-fFrom+1);
    	$("#tablePackageDetailsList tr").eq(trIndex).find(".hiddenPackageListCartonQuantity").val(fTo-fFrom+1);
    	
    	countMoreCarton(trIndex);
    }
    
    // 箱单明细-箱号form改变
    function changeCartonFromTo(inputObj,trIndex){
    	
    	// 格式化
    	formatInputNumber(inputObj,trIndex,0);
    	
    	// 改变箱数
    	changeDetailsCartonNumber(trIndex);
    	
    	// 设定箱号存在flg
    	var fFrom = parseInt($("#tablePackageDetailsList tr").eq(trIndex).find(".inputPackageListCartonNoFrom").val());
    	var fTo = parseInt($("#tablePackageDetailsList tr").eq(trIndex).find(".inputPackageListCartonNoTo").val());
    	setSameCartonNoFlg(trIndex,fFrom,fTo);
    	
    	// 计算纸箱总数
    	cartonAllQuantity();
    	
    	// 待装箱数
    	waitCartonQuantity();
    	
    	// 计算总值
    	sumAllValue();
    }
    
    // 箱号是否存在,设定箱号存在flg
    function setSameCartonNoFlg(trIndex,pFrom,pTo){

    	var arr = $("#tablePackageDetailsList tr");
    	var sameFlg = 0;
    	$.each(arr, function(i, item){
    		var rowFrom = parseInt($(item).find(".inputPackageListCartonNoFrom").val());
    		var rowTo = parseInt($(item).find(".inputPackageListCartonNoTo").val());
     		if(rowFrom == pFrom && rowTo == pTo){
     			if(sameFlg==0){
     				$("#tablePackageDetailsList tr").eq(i).find(".hiddenPackageListSameCartonFlg").val(0);
     				sameFlg = 1;
     			}else{
     				$("#tablePackageDetailsList tr").eq(i).find(".hiddenPackageListSameCartonFlg").val(1);
     			}
    		}
    	});    	
    }
    
    // 每箱件
    function oneCartonQuantity(trIndex){
    	var arr = $("#tablePackageDetailsList tr").eq(trIndex).find(".subSize");
    	var vCount = 0;
    	$.each(arr, function(i, item){
    		var sizeQuantity = $(item).text();
    		if(sizeQuantity == null || sizeQuantity == '' || sizeQuantity == "undefined"){
    			sizeQuantity = 0;
    		}
    		
    		vCount = vCount + parseInt(sizeQuantity);
    	});
    	
    	$("#tablePackageDetailsList tr").eq(trIndex).find(".tdPackageListSingleCarton").text(vCount);
    	$("#tablePackageDetailsList tr").eq(trIndex).find(".hiddenPackageListSingleCarton").val(vCount);
    	
    	countMoreCarton(trIndex);
    	
    	waitCartonQuantity();
    	
    	sumAllValue();
    }
    
    // 明细总件数
    function countMoreCarton(trIndex){
    	var fSingCarton = $("#tablePackageDetailsList tr").eq(trIndex).find(".hiddenPackageListSingleCarton").val();
    	var fCartonQuantity = $("#tablePackageDetailsList tr").eq(trIndex).find(".hiddenPackageListCartonQuantity").val();
    	$("#tablePackageDetailsList tr").eq(trIndex).find(".tdPackageListMoreCarton").text(parseInt(fSingCarton)*parseInt(fCartonQuantity));
    	$("#tablePackageDetailsList tr").eq(trIndex).find(".hiddenPackageListMoreCarton").val(parseInt(fSingCarton)*parseInt(fCartonQuantity));
    	
    }
    // 待装箱数
    function waitCartonQuantity(){
    	var arrIndentList = $("#tableIndentList tr");
    	var arrDetailsList = $("#tablePackageDetailsList tr");
    	
    	$.each(arrIndentList, function(i, itemIndent){
    		var indentColor = $(itemIndent).find(".styleInfoColor").text();
    	    var indentSize = $(itemIndent).find(".styleInfoSizeName").text();
    	    var waitCount = 0;
    	    
    		$.each(arrDetailsList, function(j, itemDetails){
    			var detailsColor = $(itemDetails).find(".selectPackageListColor").eq(1).val();
    			if(indentColor == detailsColor){
    			   var arrDetailsSizeName = $(itemDetails).find(".hiddenSubSizeName");
    			   var sizeIndex = 9999;
    			   $.each(arrDetailsSizeName, function(k, itemSize){
    				   var detailsSizeName = $(itemSize).val();
    				   if(detailsSizeName != null && detailsSizeName != '' && detailsSizeName != "undefined"){
	    				   if(indentSize == detailsSizeName){
	    					   sizeIndex = k;
	    				   }
    				   }
    			   });
    			   
    			   if(sizeIndex!=9999){
    				   var tempCount = $(itemDetails).find(".hiddenSubSize").eq(sizeIndex).val();
    		    		if(tempCount == null || tempCount == '' || tempCount == "undefined"){
    		    			waitCount += 0;
    		    		}else{
    		    			var tempPackageNum = $(itemDetails).find(".hiddenPackageListCartonQuantity").val();
    				        waitCount = waitCount + parseInt(tempCount)*tempPackageNum;
    		    		}
    			   }
    			}
    		});
    		var old = $(itemIndent).find(".hiddenIndentListIndentNumber").val();
    		$(itemIndent).find(".tdIndentListUnPackNumber").text(old-waitCount);
    		$(itemIndent).find(".hiddenIndentListUnPackNumber").val(old-waitCount);
    	});
    }
    // 纸箱总数
    function cartonAllQuantity(){
    	var arrCartonList = $("#tableCartonList tr");
    	var arrDetailsList = $("#tablePackageDetailsList tr");
    	
    	$.each(arrCartonList, function(i, itemCarton){
    		var vCartonId = $(itemCarton).find(".hiddenCartonInfoListId").val();
    		var vCount = 0;
    		$.each(arrDetailsList, function(j, itemDetails){
    			var detailsCartonId = $(itemDetails).find(".selectPackageListCartonId").eq(1).val();
    			var detailsCartonFlg = $(itemDetails).find(".hiddenPackageListSameCartonFlg").val();
   			    if(vCartonId == detailsCartonId && detailsCartonFlg==0){
 				   var tempCount = $(itemDetails).find(".hiddenPackageListCartonQuantity").val();
 				   vCount += parseInt(tempCount);
   			    }
    		});
    		$(itemCarton).find(".tdCartonInfoListCartonNumber").text(vCount);
    		$(itemCarton).find(".hiddenCartonInfoListCartonNumber").val(vCount);
    	});
    }
    
    // 纸箱名称改变
    function changeSelectPackageListCarton(trIndex){
    	var detailsCartonId = $("#tablePackageDetailsList tr").eq(trIndex).find(".selectPackageListCartonId").eq(1).val();
    	var arrCartonList = $("#tableCartonList tr");
    	$.each(arrCartonList, function(i, itemCarton){
    		var vCartonId = $(itemCarton).find(".hiddenCartonInfoListId").val();
    		if(vCartonId != null && vCartonId != '' && vCartonId != "undefined"){
    			if(vCartonId==detailsCartonId){
    				var vVolume = $(itemCarton).find(".hiddenCartonInfoListCartonVolume").val();
    				var vWeight = $(itemCarton).find(".hiddenCartonInfoListCartonWeight").val();
    				$("#tablePackageDetailsList tr").eq(trIndex).find(".hiddenPackageListBoxVolume").val(vVolume);
    				$("#tablePackageDetailsList tr").eq(trIndex).find(".hiddenPackageListBoxWeight").val(vWeight);
    			}
    		}
    	});
    	
    	cartonAllQuantity();
    	sumAllValue();
    }
    
    // 计算 所有总数(总件数,总箱数,总毛重,总净重,总体积)
    function sumAllValue(){
    	var arr = $("#tablePackageDetailsList tr");
    	// 总件数
    	var vPackageStyleQuantity = 0;
    	// 总箱数
    	var vPackageQuantity = 0;
    	// 总净重
    	var vPackageNetWeight = 0.00;
    	// 总体积
    	var vPackageVolume = 0.00;
    	// 总毛重
    	var vPackageRoughWeight = 0.00;
    	
    	$.each(arr, function(i, item){
       		// 总箱数
       		var eachPackageQuantity = parseInt($(item).find(".hiddenPackageListCartonQuantity").val());
       		
    		// 总件数
    		var eachPackageStyleQuantity = parseInt($(item).find(".hiddenPackageListMoreCarton").val());
    		vPackageStyleQuantity += eachPackageStyleQuantity;
    		
    		// 总净重
    		var eachPackageNetWeight = Math.formatFloat(parseFloat($(item).find(".telPackageListCartonNetWeight").val())*eachPackageQuantity,2);
    		vPackageNetWeight = Math.formatFloat((vPackageNetWeight + eachPackageNetWeight),2);
    		
    		var detailsCartonFlg = $(item).find(".hiddenPackageListSameCartonFlg").val();
    		if(detailsCartonFlg==0){
    			// 总箱数
        		vPackageQuantity += eachPackageQuantity;
        		// 总体积
        		var eachPackageListBoxVolume = parseFloat($(item).find(".hiddenPackageListBoxVolume").val());
        		vPackageVolume = Math.formatFloat(vPackageVolume + (eachPackageListBoxVolume * eachPackageQuantity),2);
   		    	// 箱子重量
    		    var eachPackageRoughWeight = parseFloat($(item).find(".hiddenPackageListBoxWeight").val());
        		vPackageRoughWeight = Math.formatFloat(vPackageRoughWeight + (eachPackageRoughWeight * eachPackageQuantity),2);
    		}
        });
    	// 总件数
    	$("#pagePackageStyleValue span").find(".pagePackageStyleQuantity").text(vPackageStyleQuantity);
    	$("#packageStyleQuantity").val(vPackageStyleQuantity);
    	
    	// 总箱数
    	$("#pagePackageStyleValue span").find(".pagePackageQuantity").text(vPackageQuantity);
    	$("#packageQuantity").val(vPackageQuantity);
    	
    	// 总毛重
    	$("#pagePackageStyleValue span").find(".pagePackageRoughWeight").text(Math.formatFloat((vPackageRoughWeight+vPackageNetWeight),2));
    	$("#packageRoughWeight").val(Math.formatFloat((vPackageRoughWeight+vPackageNetWeight),2));
    	
    	// 总净重
    	$("#pagePackageStyleValue span").find(".pagePackageNetWeight").text(vPackageNetWeight);
    	$("#packageNetWeight").val(vPackageNetWeight);
    	
    	// 总体积
    	$("#pagePackageStyleValue span").find(".pagePackageVolume").text(vPackageVolume);
    	$("#packageVolume").val(vPackageVolume);
    }
    
	Math.formatFloat = function(f, digit) { 
		var s = f;
		//判断字符串是否为数字     //判断正整数 /^[1-9]+[0-9]*]*$/  
		var numberVerification = /^[0-9]+.?[0-9]*$/;   
		if (numberVerification.test(s)){
			s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(digit) + "";  
		}else{
			s = 0;
		}
	    return parseFloat(s); 
	}
	
    //保存方法
    var saveForm={
         t:null,
         save:function(t){
        	 /* if (!confirm('<spring:message code="common_save"/>')) {
        		 return;
        	 } */
        	 
        	 /* var hasSize = isEmptyStyleQuantity();
        	 if(hasSize){
        		 layer.msg('<spring:message code="enchase_pleaseInputSize"/>', {time:1000});
        		 return;
        	 } */
        	 $("#activity_pane").showLoading();
             $(t).attr("disabled",true);
             this.t=t;
	       	 // 格式化，去除逗号
	         formUnFormat($("#inputForm"));
	         // 从表单获取数据信息
	         var params = $("#inputForm").serialize();
	         // 调用后台保存方法
	         $.post("${ctx}/customenchase/single/save",params,function(result){
	             // 提示信息
	             tipParent(result.message);
	             // 返回状态
	             if(result.statusCode==STATUSCODEOK){
	                  window.parent.frames[0].searchListForm();
	                  closeWin(); 
	             }else{
	                  $(saveForm.t).attr("disabled",false);
	             }
	             $("#activity_pane").hideLoading();
	         },'json'); 
         } 
    }
    
    // 每箱尺寸搭配,必须入力
    function isEmptyStyleQuantity(){
    	
    	var arr = $("#tablePackageDetailsList tr");
    	
    	if($("#tablePackageDetailsList tr").length==0){
    		return true;
    	}
    	
    	var hasFlg = false;
    	$.each(arr, function(i, item){
    		var subSizeList = $(item).find(".hiddenSubSize");
    		var roHasFlg = false;
    		if(subSizeList.length==0){
    			hasFlg = true;
    		}else{
	    		$.each(subSizeList, function(j, itemSize){
	        		var temp = $(itemSize).val();
	        		if(temp!=null && temp!='' && temp!="undefined" && parseInt(temp)>0){
	        		   roHasFlg = true;
	        		}
	    		});
	    		
	    		if(!roHasFlg){
	    		   hasFlg = true;
	    		}
    		}
    	});
    	
    	return hasFlg;
    }
    
    function enchaseCloud() {
    	if ($("#id").val()==null||$("#id").val()=="") {
	    	var cartonLength = $("#tableCartonList").find("tr").length;
	    	if (cartonLength!=2) {
	    		tipParent("请选择箱子规格");
				return;
			}
			var deliveryDate = $("#deliveryDate").val();
			if (deliveryDate == null || deliveryDate == "") {
				tipParent("请选择交货日期");
				return;
			}
			var customSel = $("#customSel").val();
			if (customSel == null || customSel == "") {
				tipParent("请选择客户");
				return;
			}
    	}
    	if ($("#id").val()==null||$("#id").val()=="") {
    		$("#activity_pane").showLoading();
    	}
    	var id = $("#id").val();
    	var ognizationId = $("#customSel").find("option:selected").attr("data-value");
    	var ognizationSimpleName = $("#customSel").find("option:selected").val();
		var targetDateStr = $("#deliveryDate").val();
		var bigCartonId = $(".hiddenCartonInfoListId")[1].value;
		var smallCartonId = $(".hiddenCartonInfoListId")[0].value;
		var bigCartonWeight = $(".hiddenCartonInfoListCartonWeight")[1].value;
		var smallCartonWeight = $(".hiddenCartonInfoListCartonWeight")[0].value;
		var bigCartonVolume = $(".hiddenCartonInfoListCartonVolume")[1].value;
		var smallCartonVolume = $(".hiddenCartonInfoListCartonVolume")[0].value;
		var bigCartonType = $(".cartonInfoName")[1].value;
		bigCartonType = bigCartonType.split("-")[1];
		var smallCartonType = $(".cartonInfoName")[0].value;
		smallCartonType = smallCartonType.split("-")[1];
		$.post("${ctx}/customenchase/single/makeEnchaseList?id="+id
				+"&ognizationPEntity.ognizationId="+ognizationId
				+"&ognizationPEntity.ognizationSimpleName="+ognizationSimpleName
				+"&bigCartonId="+bigCartonId
				+"&smallCartonId="+smallCartonId
				+"&bigCartonType="+bigCartonType
				+"&smallCartonType="+smallCartonType
				+"&targetDateStr="+targetDateStr
				+"&bigCartonWeight="+bigCartonWeight
				+"&smallCartonWeight="+smallCartonWeight
				+"&bigCartonVolume="+bigCartonVolume
				+"&smallCartonVolume="+smallCartonVolume,"",function(result){
			// 回调函数赋值
            var h = $(callBackHtml(result)).find("#divHtml").html();
            $('#divHtml').html(h);
            var y = $(callBackHtml(result)).find("#tableIndentList").html();
            $('#tableIndentList').html(y);
            $("#btnSave").show();
            var smallCount = $("#smallCount").val();
            var bigCount = $("#bigCount").val();
            $(".tdCartonInfoListCartonNumber")[0].innerHTML = smallCount;
            $(".tdCartonInfoListCartonNumber")[1].innerHTML = bigCount;
            setIndex();
        	$("#activity_pane").hideLoading();
        	if($("#tablePackageDetailsList tr").length==0){
        		$("#btnSave").hide();
        		var errorMessage = $(callBackHtml(result)).find("#errorMessage").val();
        		if (errorMessage != "") {
	        		alert(errorMessage);
				}else{
					tipParent("无数据");
				}
            }
        },'html'); 
	}
    
    var tempIndex;
	function setIndex(){
        var reg=/tempIndex/g;
		var trOp = $("#tablePackageDetailsList tr").length;
		if (trOp > 0) {
			for (var i = 0; i < trOp; i++) {
                    var tableFit = $("#tablePackageDetailsList tr").eq(i);
					$(tableFit).html($(tableFit).html().replace(reg,i));
			}
		}
	}
	function saveData(t){
		$("#activity_pane").showLoading();
   	 $(t).attr("disabled",true);
   	var action=$("#inputForm").attr("action") + "?ognizationPEntity.ognizationId="+$("#customSel").find("option:selected").attr("data-value");
   	var form = document.getElementById('inputForm'); 
       var fd = new FormData(form);
       var xhr = new XMLHttpRequest();
           // 文件上传成功或是失败
           xhr.onreadystatechange = function(e) {
           	
               if (xhr.readyState == 4) {
               	
               	var result=JSON.parse(xhr.responseText);
               	 // 提示信息
                   alertInfo(result.message);
               	
                   if (xhr.status == 200) {
                       // 返回状态
                       if(result.statusCode==STATUSCODEOK){
                               // 刷新页面
                               window.parent.frames[0].searchListForm();
                               // 关闭页面
                               closeWin(); 
                         }else{
                             $(t).attr("disabled",false);
                         }
                       $("#activity_pane").hideLoading();
                         xhr=null;
                   } 
               }
           };
           
           // 开始上传
           xhr.open("POST", action, true);
           xhr.send(fd);
   	
   		return false;
   }

    </script> 
</head>
<body id="activity_pane">   
<form id="inputForm" action="${ctx}/customenchase/single/save" method="post" onsubmit="formSubmit(this.action)" enctype="multipart/form-data">
<div class="layerHeader clearfix" id="activity_pane">
	<c:if test="${enchase.id == null || enchase.id == '' }">
		<p class="headerTitle"><spring:message code="enchase_create_title"/></p>
	</c:if>
	<c:if test="${enchase.id != null && enchase.id != '' }">
		<p class="headerTitle">编辑装箱单</p>
	</c:if>
    <div class="headerButton clearfix">
    	<div style="float: left;padding: 0px;">
	    	<select id="customSel" name="ognizationPEntity.ognizationSimpleName" style="width: 150px;">
		        <option value="">请选择客户</option>
		        <c:forEach items="${customList }" var="custom">
		        	<option <c:if test="${(enchase.ognizationPEntity.ognizationId) eq custom.id }">selected="selected"</c:if>
		        	 value="${custom.ognizationSimpleName }" data-value="${custom.id }">${custom.ognizationName }</option>
		        </c:forEach>
	        </select>
        </div>
    	<div style="float: left;padding: 5px;">
    		<input type="text" onblur="CheckDate(this);" 
								id="deliveryDate" name="targetDateStr" placeholder="交货日期" style="width: 100px" value="<fmt:formatDate value="${enchase.targetDate}" pattern="yyyy-MM-dd"/>">
		</div>
    	<button type="button" onclick="enchaseCloud();">生成箱单</button>
        <button type="button" onclick="popup('650px','519px','${ctx}/customenchase/single/cartonSelect')"><spring:message code="enchase_create_btn_selectCarton"/></button>
        <%-- <button type="button" onclick="popup('800px','519px','${ctx}/purchase/handbook/indentSelect')"><spring:message code="enchase_create_btn_selectStyle"/></button> --%>
        <button type="button" id="btnSave" onclick="saveData(this)"><spring:message code="common_save"/></button>
        <button type="button" onclick="closeWin()"><spring:message code="btn_cancle"/></button>
    </div>
</div>
<c:set value="<%=ResourcesUtil.getConfig(\"customer_country\")%>" var="customerCountry" />
<c:set value="<%=ResourcesUtil.getConfig(\"package_style\")%>" var="packageStyle" />
<input type="hidden" id="commaCartonId" name="commaCartonId" value="${enchase.commaCartonId}"/>
<input type="hidden" id="trIndex" name="trIndex" value="${enchase.trIndex}"/>
<input type="hidden" id="id" name="id" value="${enchase.id}"/>
<div class="tabBoxSm">
 <div class="tabSm" style="padding: 10px 5px;">
  <div style="height:180px;"> 
    <!-- 合同信息 -->
    <div class="left col-50">
         <p style="margin-top:0;margin-bottom:5px;margin-left:20px;"><span style="font-size:16px;">合同信息</span></p>
         <div class="tableArea">
        	<div class="tableScroll tableScrollSm" style="width:470px;height:142px;">
        		<table cellpadding="0" class="tableHead">
                    <thead>
                    <tr>	
                        <!-- No. -->	
                    	<th style="width:40px;"><spring:message code="enchase_show_styleInfo_no"/></th>
                    	<!-- 合同单号 -->
                        <th><p style="width:250px;"><spring:message code="enchase_show_styleInfo_customerContractNo"/></p></th>
                        <!-- 总件数 -->
                        <th style="width:160px;"><spring:message code="enchase_show_styleInfo_allQuantity"/></th>
                    </tr>
                    </thead>
                </table>
                <table cellpadding="0" class="tableBody" id="tableIndentList">
                <c:if test="${not empty enchase.contracts}">
                    <tbody>
                    <c:forEach items="${enchase.contracts}" var="contract" varStatus="status">
                    <tr>
                        <!-- NO -->
                    	<td style="width:40px;">${status.count}</td>
                    	<!-- 合同单号 -->
                        <td><p style="width:250px;" class="text_overflow" data-toggle='tooltip' title='${contract.contractNo}'>${contract.contractNo}</p></td>
                        <input type="hidden" name="indentList[${status.index}].customerContractNo" value="${contract.contractNo}"/>
                        <!-- 总件数 -->
                        <td style="width:160px;"><fmt:formatNumber value="${contract.contractCount}" pattern="#,##0"/></td>
                        <input  class="hiddenIndentListIndentNumber" type="hidden" name="indentList[${status.index}].indentNumber" value="${contract.contractCount}"/>
                    </tr>
                    </c:forEach>
                    </tbody>
                </c:if>
                </table>
            </div>
        </div>
    </div>
    <!-- 纸箱信息 -->
    <div class="right col-50">
         <p style="margin-top:0;margin-bottom:5px;margin-left:20px;"><span style="font-size:16px;"><spring:message code="enchase_show_cartonInfo_title"/></span></p>
         <div class="tableArea">
        	<div class="tableScroll tableScrollSm" style="width:484px;height:142px;">
        		<table cellpadding="0" class="tableHead">
                    <thead>
                    <tr>
                        <!-- No. -->
                    	<th style="width:30px;"><spring:message code="enchase_show_cartonInfo_no"/></th>
                    	<!-- 箱体名称 -->
                        <th style="width:100px;"><spring:message code="enchase_show_cartonInfo_cartonName"/></th>
                        <!-- 长 -->
                        <th style="width:55px;"><spring:message code="enchase_show_cartonInfo_length"/></th>
                        <!-- 宽 -->
                        <th style="width:55px;"><spring:message code="enchase_show_cartonInfo_width"/></th>
                        <!-- 高 -->
                        <th style="width:55px;"><spring:message code="enchase_show_cartonInfo_height"/></th>
                        <!-- 体积 -->
                        <th style="width:55px;"><spring:message code="enchase_show_cartonInfo_volume"/></th>
                        <!-- 箱重 -->
                        <th style="width:55px;"><spring:message code="enchase_show_cartonInfo_weight"/></th>
                        <!-- 总数 -->
                        <th style="width:60px;"><spring:message code="enchase_show_cartonInfo_allQuantity"/></th>
                    </tr>
                    </thead>
                </table>
                <table cellpadding="0" class="tableBody" id="tableCartonList">
                <c:if test="${not empty enchase.cartonInfoList}">
                    <tbody id="cartonHtml">
                    <c:forEach items="${enchase.cartonInfoList}" var="cartonInfo" varStatus="status">
                    	<tr>
                    	    <!-- NO -->
							<td style="width:30px;">${status.index+1}</td>
							<!-- 箱体名称 -->
							<td style="width:100px;">${cartonInfo.cartonName}</td>
							<input class="cartonInfoName" type="hidden" value="${cartonInfo.cartonName}"/>
							<!-- 长 -->
							<td style="width:55px;"><fmt:formatNumber value="${cartonInfo.cartonLength}" pattern="#,##0.####"/></td>
							<input type="hidden" value="${cartonInfo.cartonLength}"/>
							<!-- 宽 -->
							<td style="width:55px;"><fmt:formatNumber value="${cartonInfo.cartonWidth}" pattern="#,##0.####"/></td>
							<input type="hidden" value="${cartonInfo.cartonWidth}"/>
							<!-- 高 -->
							<td style="width:55px;"><fmt:formatNumber value="${cartonInfo.cartonHeight}" pattern="#,##0.####"/></td>
							<input type="hidden" value="${cartonInfo.cartonHeight}"/>
							<!-- 体积 -->
							<td style="width:55px;"><fmt:formatNumber value="${cartonInfo.cartonVolume}" pattern="#,##0.####"/></td>
							<input class="hiddenCartonInfoListCartonVolume" type="hidden" value="${cartonInfo.cartonVolume}"/>
							<!-- 箱重 -->
							<td style="width:55px;"><fmt:formatNumber value="${cartonInfo.cartonWeight}" pattern="#,##0.####"/></td>
							<input class="hiddenCartonInfoListCartonWeight" type="hidden" value="${cartonInfo.cartonWeight}"/>
							<!-- 总数 -->
							<td class="tdCartonInfoListCartonNumber" style="width:60px;"><fmt:formatNumber value="${cartonInfo.cartonNumber}" pattern="#,##0"/></td>
							<input class="hiddenCartonInfoListCartonNumber" type="hidden" value="${cartonInfo.cartonNumber}"/>
							<!-- id -->
							<td class="none">${cartonInfo.id}</td>
							<input class="hiddenCartonInfoListId" type="hidden" value="${cartonInfo.id}"/>
						</tr>
					</c:forEach>	
                    </tbody>
                </c:if>    
                </table>
            </div>
        </div>
    </div>
  </div>
  <div id="divHtml">
      <div class="items clearfix" id="pagePackageStyleValue">
	      <p style="margin-top:0;margin-bottom:0px;margin-left:10px;"><span style="font-size:16px;"><spring:message code="enchase_show_package_title"/></span>
	      <span style="color:#d6392d;right:650px;position:absolute;"><spring:message code="enchase_show_package_allStyleQuantity"/><span class="pagePackageStyleQuantity"><fmt:formatNumber value="${enchase.contractCount}" pattern="#,##0"/></span></span>
	      <span style="color:#d6392d;right:500px;position:absolute;"><spring:message code="enchase_show_package_allCartonQuantity"/><span class="pagePackageQuantity"><fmt:formatNumber value="${enchase.bigCount+enchase.smallCount}" pattern="#,##0"/></span></span>
	      <span style="color:#d6392d;right:350px;position:absolute;"><spring:message code="enchase_show_package_allRoughtWeight"/><span class="pagePackageRoughWeight"><fmt:formatNumber value="${enchase.grossWeight}" pattern="#,##0.000#"/></span></span>
	      <span style="color:#d6392d;right:200px;position:absolute;"><spring:message code="enchase_show_package_allNetWeight"/><span class="pagePackageNetWeight"><fmt:formatNumber value="${enchase.netWeight}" pattern="#,##0.000#"/></span></span>
	      <span style="color:#d6392d;right:50px;position:absolute;"><spring:message code="enchase_show_package_allVolume"/><span class="pagePackageVolume"><fmt:formatNumber value="${enchase.volume}" pattern="#,##0.000#"/></span></span>
	      <input type="hidden" id="bigCount" name="bigCount" value="${enchase.bigCount}"/>
	      <input type="hidden" id="smallCount" name="smallCount" value="${enchase.smallCount}"/>
	      <input type="hidden" id="contractCount" name="contractCount" value="${enchase.contractCount}"/>
	      <input type="hidden" id="volume" name="volume" value="${enchase.volume}"/>
	      <input type="hidden" id="netWeight" name="netWeight" value="${enchase.netWeight}"/>
	      <input type="hidden" id="grossWeight" name="grossWeight" value="${enchase.grossWeight}"/>
	      <input type="hidden" id="errorMessage" value="${errorMessage}"/>
	      </p>
      </div>
      <div class="tableArea">
     	<div class="tableScroll tableScrollSm" style="width:983px;height:420px;">
     		<table cellpadding="0" class="tableHead" style="z-index: 99">
                 <thead>
                 <tr>
                 	 <!-- 大箱号 -->
                     <th><p style="width:100px;">大箱号</p></th>
                     <!-- 小箱号 -->
                     <th><p style="width:115px;">小箱号</p></th>
                     <!-- 订单号 -->
                     <th><p style="width:140px;">订单号</p></th>
                     <!-- 目的地 -->
                     <th><p style="width:100px;">目的地</p></th>
                     <!-- 国家 -->
                     <th><p style="width:100px;">国家</p></th>
                     <!-- Transit group -->
                     <th><p style="width:100px;">Transit group</p></th>
                     <!-- 店铺 -->
                     <th><p style="width:100px;">店铺</p></th>
                     <!-- 成分 -->
                     <th><p style="width:100px;">成分</p></th>
                     <!-- 重要 -->
                     <th><p style="width:50px;">重要</p></th>
                     <!-- 袖子 -->
                     <th><p style="width:50px;">袖子</p></th>
                 </tr>
                 </thead>
             </table>
             <table cellpadding="0" id="tablePackageDetailsList">
                <c:if test="${not empty enchase.enchases}">
                    <tbody>
                    <c:forEach items="${enchase.enchases}" var="bigEnchase" varStatus="bigStatus">
                    <c:forEach items="${bigEnchase.enchases}" var="smallEnchase" varStatus="smallStatus">
                    <c:forEach items="${smallEnchase.enchaseList}" var="enchase" varStatus="status">
                    <tr>
                     <!-- 大箱号 -->
                     <c:if test="${smallStatus.index == 0 && status.index == 0 }">
                     	<td rowspan="${bigEnchase.rowSpan }"><p style="width:100px;" class="text_overflow" title="${bigEnchase.bigCartonNo }" data-toggle="tooltip">${bigEnchase.bigCartonNo }</p></td>
                     </c:if>
                     <!-- 小箱号 -->
                     <c:if test="${status.index == 0 }">
                     	<td rowspan="${fn:length(smallEnchase.enchaseList)}"><p style="width:115px;" class="text_overflow" title="${smallEnchase.smallCartonNo }" data-toggle="tooltip">${smallEnchase.smallCartonNo }</p></td>
                     </c:if>
                     <!-- 订单号 -->
                     <td><p style="width:140px;" class="text_overflow" title="${enchase.orderNO }" data-toggle="tooltip">${enchase.orderNO }</p></td>
                     <!-- 目的地 -->
                     <td><p style="width:100px;" class="text_overflow" title="${enchase.shippingDestintion }" data-toggle="tooltip">${enchase.shippingDestintion }</p></td>
                     <!-- 国家 -->
                     <td><p style="width:100px;" class="text_overflow" title="${enchase.countryName }" data-toggle="tooltip">${enchase.countryName }</p></td>
                     <!-- Transit group -->
                     <td><p style="width:100px;" class="text_overflow" title="${enchase.transitGroup }" data-toggle="tooltip">${enchase.transitGroup }</p></td>
                     <!-- 店铺 -->
                     <td><p style="width:100px;" class="text_overflow" title="${enchase.shopName }" data-toggle="tooltip">${enchase.shopName }</p></td>
                     <!-- 成分 -->
                     <td><p style="width:100px;" class="text_overflow" title="${enchase.materialComposition }" data-toggle="tooltip">${enchase.materialComposition }</p></td>
                     <!-- 重要 -->
                     <td><p style="width:50px;<c:if test="${enchase.urgentCD == 'ORDER_FLAG-express_20' }">color: red;</c:if>">${fns:getDictName(enchase.urgentCD) }</p></td>
                     <!-- 袖子 -->
                     <td><p style="width:50px;"><c:if test="${enchase.sleeveFlag == 0 }">长袖</c:if><c:if test="${enchase.sleeveFlag == 1 }"><p style="color: red;">短袖</p></c:if></p></td>
                     <input type="hidden" id="bigCartonNo" name="enchaseList[tempIndex].bigCartonNo" value="${bigEnchase.bigCartonNo }"/>
                     <input type="hidden" id="smallCartonNo" name="enchaseList[tempIndex].smallCartonNo" value="${smallEnchase.smallCartonNo }"/>
                     <input type="hidden" id="contractID" name="enchaseList[tempIndex].contractID" value="${enchase.contractID }"/>
                     <input type="hidden" id="orderId" name="enchaseList[tempIndex].id" value="${enchase.id }"/>
                     <input type="hidden" id="styleId" name="enchaseList[tempIndex].styleId" value="${enchase.styleId }"/>
                     <input type="hidden" id="orderNO" name="enchaseList[tempIndex].orderNO" value="${enchase.orderNO }"/>
                     <input type="hidden" id="shippingDestintion" name="enchaseList[tempIndex].shippingDestintion" value="${enchase.shippingDestintion }"/>
                     <input type="hidden" id="countryName" name="enchaseList[tempIndex].countryName" value="${enchase.countryName }"/>
                     <input type="hidden" id="transitGroup" name="enchaseList[tempIndex].transitGroup" value="${enchase.transitGroup }"/>
                     <input type="hidden" id="shopName" name="enchaseList[tempIndex].shopName" value="${enchase.shopName }"/>
                     <input type="hidden" id="materialComposition" name="enchaseList[tempIndex].materialComposition" value="${enchase.materialComposition }"/>
                     <input type="hidden" id="sleeveFlag" name="enchaseList[tempIndex].sleeveFlag" value="${enchase.sleeveFlag }"/>
                     <input type="hidden" id="urgentCD" name="enchaseList[tempIndex].urgentCD" value="${enchase.urgentCD }"/>
                    </tr>
                    </c:forEach>
                    </c:forEach>
                    </c:forEach>
                    </tbody>
                </c:if>   
             </table>
         </div>
      </div>
  </div>
 </div>
</div>
</form>
</body>
</html>